package org.example.datastructure;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import java.util.Random;

public class ArrayQueueDemo {
    /**
     * 数组模拟队列
     */
    @ParameterizedTest(name = "isRing={0}")
    @DisplayName("测试Queue")
    @ValueSource(booleans = {true, false})
    public void testArrayRingQueue(boolean isRing) {
        Queue arrayQueue = null;
        if (isRing) {
            arrayQueue = new ArrayRingQueue(3);
        } else {
            arrayQueue = new ArrayQueue(3);
        }
        boolean loop = true;
        int count = 0;
        Random random = new Random();

        System.out.println("显示队列");
        arrayQueue.showQueue();
        System.out.println("添数据");
        for (int i = 0; i < 4; i++) {
            int value = random.nextInt();
            System.out.printf("添加的数据是%d\n", value);
            arrayQueue.addQueue(value);
        }
        arrayQueue.showQueue();
        System.out.println("取数据");

        for (int i = 0; i < 3; i++) {
            System.out.printf("取出队列的数据是%d\n", arrayQueue.getQueue());
        }
        System.out.println("查看队列头");
        try {
            int res = arrayQueue.headQueue();
            System.out.printf("队列的数据是%d\n", res);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(e.getMessage());
        }
        System.out.println("------------");

        System.out.println("程序退出");
    }

    public interface Queue {
        boolean isFull();

        boolean isEmpty();

        void addQueue(int n);

        int getQueue();

        void showQueue();

        int headQueue();
    }

    /**
     * 只能顺序使用
     */
    public static class ArrayQueue implements Queue {
        private final int maxSize;
        private final int[] arr;//存放数据
        private int front;//队头，指向第一个元素前面一个元素
        private int rear;//队尾，指向最后的一个元素

        public ArrayQueue(int arrMaxSize) {
            maxSize = arrMaxSize;
            arr = new int[maxSize];
            front = -1;
            rear = -1;
        }

        /**
         * 判满
         *
         * @return
         */
        @Override
        public boolean isFull() {
            return maxSize == rear + 1;
        }

        /**
         * 判空
         *
         * @return
         */
        @Override
        public boolean isEmpty() {
            return rear == front;
        }

        /**
         * 添加数据
         *
         * @param n
         */
        @Override
        public void addQueue(int n) {
            if (isFull()) {
                System.out.println("队列满");
                return;
            }
            rear++;
            arr[rear] = n;
        }

        /**
         * 获取数据
         *
         * @return
         */
        @Override
        public int getQueue() {
            if (isEmpty()) {
                throw new RuntimeException("队列空，不能取数据");
            }
            front++;
            return arr[front];
        }

        /**
         * 显示数据
         */
        @Override
        public void showQueue() {
            if (isEmpty()) {
                System.out.println("队列空，没有数据");
            }
            for (int i = 0; i < arr.length; i++) {
                System.out.printf("arr[%d]=%d\n", i, arr[i]);
            }
        }

        /**
         * 显示队列头
         */
        @Override
        public int headQueue() {
            if (isEmpty()) {
                System.out.println("队列空，没有数据");
                return 0;
            }
            return arr[front + 1];
        }

    }

    /**
     * 循环队列
     */
    public static class ArrayRingQueue implements Queue {
        private final int[] arr;//存放数据
        private final int maxSize;
        private int front;//队头，指向第一个元素
        private int rear;//队尾，指向最后的一个元素的后一个元素

        public ArrayRingQueue(int arrMaxSize) {
            maxSize = arrMaxSize + 1;
            arr = new int[maxSize];
            front = 0;
            rear = 0;
        }

        @Override
        public boolean isFull() {
            return front == (rear + 1) % maxSize;
        }

        @Override
        public boolean isEmpty() {
            return front == rear;
        }

        @Override
        public void addQueue(int n) {
            if (isFull()) {
                System.out.println("队列满");
                return;
            }
            arr[rear] = n;
            rear = (rear + 1) % maxSize;
        }

        @Override
        public int getQueue() {
//            if (isEmpty()) {
//                throw new RuntimeException("队列空，不能取数据");
//            }
            int tmp = arr[front];
            front = (front + 1) % maxSize;
            return tmp;
        }

        @Override
        public void showQueue() {
            if (isEmpty()) {
                System.out.println("队列空，没有数据");
            }
            int length = front + effectiveNumber();
            for (int i = front; i < length; i++) {
                System.out.printf("arr[%d]=%d\n", i % maxSize, arr[i % maxSize]);
            }
        }

        /**
         * 求出当前队列有效数据的个数
         *
         * @return
         */
        public int effectiveNumber() {
            return (maxSize + rear - front) % maxSize;
        }

        @Override
        public int headQueue() {
            if (isEmpty()) {
                System.out.println("队列空，没有数据");
                return 0;
            }
            return arr[front + 1];
        }
    }


}
